package com.zues.java_test.juc.pool_test;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;

public class CustomPool {

//    private static final ExecutorService executorService = Executors.newFixedThreadPool(35);
    private static final ThreadPoolExecutor executorService = new ThreadPoolExecutor(
                    1,
                    15,
                    0L,
                    TimeUnit.SECONDS,
                    new LinkedBlockingQueue<Runnable>()
            );

    public static void main(String[] args) throws InterruptedException {
        List<String> ipList = Arrays.asList("192.168.1.1", "192.168.1.2", "192.168.1.3", "192.168.1.4", "192.168.1.5");
//        while(true){
//            ipList.forEach(ip -> {
//                executorService.submit(() -> {
//                    while (!Thread.currentThread().isInterrupted()) {
//                        try {
//                            // 这里调用你的检查设备连接的方法
//                            System.out.println(Thread.currentThread().getName() + " : " + ip + "...连接了");
//                            // 休眠一段时间，避免CPU空转
//                            Thread.sleep(3000);
//                        } catch (InterruptedException e) {
//                            // 当线程被中断时退出循环
//                            Thread.currentThread().interrupt();
//                        }
//                    }
//                });
//            });
//        }

        for (String ip : ipList) {
            executorService.submit(() -> {
                while (!Thread.currentThread().isInterrupted()) {
                    try {
                        // 这里调用你的检查设备连接的方法
                        System.out.println(Thread.currentThread().getName() + " : " + ip + "...连接了");
                        // 休眠一段时间，避免CPU空转
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        // 当线程被中断时退出循环
                        Thread.currentThread().interrupt();
                    }
                }
            });
        }

    }

}
